#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include <QLabel>
#include <QCoreApplication>
#include <QNetworkInterface>
#include <QNetworkAddressEntry>
#include <QDebug>
#include <QVector>
#include <QFile>
#include <qdir.h>
#include <QPushButton>
#include <QKeyEvent>
#include <QFileDialog>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_setListen_clicked();

    void on_sendMsg_clicked();

    void printHostIPAddresses();

    void on_clean_clicked();

    void on_selFile_clicked();

    void on_sendFile_clicked();

private:
    Ui::MainWindow *ui;
    QTcpServer* m_s;
    //QTcpSocket* m_tcp;
    QVector<QTcpSocket*> m_tcp;

    QString MyHost;
    QString path;

    static int count;
    static int TextSum;
    int cNum = 0;
    //QLabel* m_status;

};

// 事件过滤器
class KeyFilter : public QObject {
    Q_OBJECT

public:
    explicit KeyFilter(QPushButton* push, QPushButton* clean, QPushButton* selFile, QPushButton* sendFile)
    {
        _push = push;
        _clean = clean;
        _selFile = selFile;
        _sendFile = sendFile;
    }
protected:
    bool eventFilter(QObject *obj, QEvent *event) override {
        if (event->type() == QEvent::KeyPress) {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) {
                    _push->click(); // 触发按钮的点击事件
                    return true; // 阻止事件进一步传播,直接返回
            }
            if (keyEvent->modifiers() == Qt::ControlModifier && keyEvent->key() == Qt::Key_L) {
                // 检测到 Ctrl + L 按键组合
                _clean->click();
                return true;
            }
            if (keyEvent->modifiers() == Qt::ControlModifier && keyEvent->key() == Qt::Key_N) {
                // 检测到 Ctrl + N 按键组合
                _selFile->click();
                return true;
            }
            if (keyEvent->modifiers() == Qt::ControlModifier && keyEvent->key() == Qt::Key_M) {
                // 检测到 Ctrl + M 按键组合
                _sendFile->click();
                return true;
            }
        }
        // 对于其他事件，继续正常处理
        return QObject::eventFilter(obj, event);
    }
private:
    QPushButton* _push;
    QPushButton* _clean;
    QPushButton* _selFile;
    QPushButton* _sendFile;
};
#endif // MAINWINDOW_H
